package com.ruoyi.boat.service.impl;

import com.ruoyi.boat.domain.Path;
import com.ruoyi.boat.domain.PathPoint;
import com.ruoyi.boat.mapper.PathMapper;
import com.ruoyi.boat.service.IPathService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 路径Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-01
 */
@Service
public class PathServiceImpl implements IPathService 
{
    @Autowired
    private PathMapper pathMapper;

    /**
     * 查询路径
     * 
     * @param id 路径主键
     * @return 路径
     */
    @Override
    public Path selectPathById(Integer id)
    {
        return pathMapper.selectPathById(id);
    }

    @Override
    public Path selectActivePath(String boatCode) {
        return pathMapper.selectActivePath(boatCode);
    }

    /**
     * 查询路径列表
     * 
     * @param path 路径
     * @return 路径
     */
    @Override
    public List<Path> selectPathList(Path path)
    {
        return pathMapper.selectPathList(path);
    }

    /**
     * 新增路径
     * 
     * @param path 路径
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPath(Path path)
    {
        path.setCreateTime(DateUtils.getNowDate());
        int rows = pathMapper.insertPath(path);
        insertNotSendPathPoint(path);
        return rows;
    }

    /**
     * 修改路径
     * 
     * @param path 路径
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePath(Path path)
    {
        pathMapper.deleteNotSendPathPoint(path.getId());
        insertNotSendPathPoint(path);
        return pathMapper.updatePath(path);
    }

    /**
     * 批量删除路径
     * 
     * @param ids 需要删除的路径主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePathByIds(String ids)
    {
        pathMapper.deletePathPointByPathIds(Convert.toStrArray(ids));
        return pathMapper.deletePathByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除路径信息
     * 
     * @param id 路径主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePathById(Integer id)
    {
        pathMapper.deletePathPointByPathId(id);
        return pathMapper.deletePathById(id);
    }

    /**
     * 新增路径点信息
     * 
     * @param path 路径对象
     */
    public void insertNotSendPathPoint(Path path)
    {
        List<PathPoint> pathPointList = path.getPathPointList();
        Integer id = path.getId();
        if (StringUtils.isNotNull(pathPointList))
        {
            List<PathPoint> list = new ArrayList<PathPoint>();
            for (PathPoint pathPoint : pathPointList)
            {
                if(!pathPoint.isSend()){
                    pathPoint.setPathId(id);
                    list.add(pathPoint);
                }
            }
            if (list.size() > 0)
            {
                pathMapper.batchPathPoint(list);
            }
        }
    }
}
